﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ByteSite.Common;
using SqlSugar;
using System.Linq;

namespace ByteSite.Web.admin.form
{
    public partial class data_role_edit : Web.UI.ManagePage
    {
        private readonly Entitys.sysconfig sysConfig = new BLL.sysconfig().loadConfig();//获得系统配置信息
        private string action = DTEnums.ActionEnum.Add.ToString();//操作类型
        private int id = 0;
        protected List<Entitys.dt_form_data_role_value> roleValues;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.form_data_role().Exists(this.id))
                {
                    JscriptMsg("记录不存在或已删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("data_role_list", DTEnums.ActionEnum.View.ToString()); //检查权限
                TableBind();
                MangerBind();
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
                else
                {
                    //txtName.Attributes.Add("ajaxurl", "../../tools/admin_ajax.ashx?action=channel_name_validate");
                }
                ColumnBind(sysConfig.sysdatabaseprefix + "form_data_" + ddlForm.SelectedValue);
            }
        }

        #region 绑定数据表=================================
        private void TableBind()
        {
            BLL.forms bll = new BLL.forms();
            List<Entitys.dt_forms> list = bll.GetList(x => x.id > 0, x => x.id);

            this.ddlForm.Items.Clear();
            foreach (Entitys.dt_forms model in list)
            {
                this.ddlForm.Items.Add(new ListItem(model.title, model.name.ToString()));
            }
        }

        private void ColumnBind(string table)
        {
            BLL.database bll = new BLL.database();
            List<DbColumnInfo> columnList = bll.ColumnList(table);

            this.ddlColumn.Items.Clear();
            foreach (DbColumnInfo model in columnList)
            {
                this.ddlColumn.Items.Add(new ListItem(model.DbColumnName.ToString(), model.DbColumnName.ToString()));
            }
        }
        private void MangerBind()
        {
            BLL.manager bll = new BLL.manager();
            List<Entitys.dt_manager> list = bll.GetList(x => x.is_audit == 0 && x.is_lock == 0, x => x.id);

            this.ddlManagers.Items.Clear();
            foreach (Entitys.dt_manager model in list)
            {
                this.ddlManagers.Items.Add(new ListItem(model.real_name.ToString()+" - "+ model.user_name, model.id.ToString()));
            }
        }
        #endregion


        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            Entitys.dt_form_data_role model = new BLL.form_data_role().GetModel(_id);

            ddlForm.SelectedValue = model.form_id.ToString();
            hidXmSelectddlManagers.Value = model.user_id.ToString();
            txtSql.Text = model.sql;
            txtTitle.Text = model.title;
            if (model.role_value_list != null)
            {
                if (model.role_value_list.Count > 0)
                {
                    roleValues = model.role_value_list;
                }

            }

        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            int result = 0;
            int managers = 0;
            Entitys.dt_form_data_role model = new Entitys.dt_form_data_role();
            BLL.form_data_role bll = new BLL.form_data_role();
            List<Entitys.dt_form_data_role_value> vl = new List<Entitys.dt_form_data_role_value>();
            List<Entitys.TableRole> roles = JsonHelper.JSONToObject<List<Entitys.TableRole>>(hidWhereJson.Value);
            if (roles != null)
            {
                if (roles.Count > 0)
                {
                    foreach (Entitys.TableRole role in roles)
                    {
                        Entitys.dt_form_data_role_value r = new Entitys.dt_form_data_role_value();

                        r.add_time = r.update_time = DateTime.Now;
                        r.add_user = r.update_user = GetAdminInfo().id;
                        r.column = role.column;
                        r.delete_flag = 0;
                        r.role_id = 0;
                        r.type = role.type;
                        r.value = role.value;
                        vl.Add(r);
                    }
                }
            }

            foreach (string uid in ddlManagers.SelectedValue.Split(','))
            {
                managers++;
                model.add_time = model.update_time = DateTime.Now;
                model.add_user = model.update_user = GetAdminInfo().id;
                model.delete_flag = 0;
                model.form_id = new BLL.forms().GetModel(ddlForm.SelectedValue).id;
                model.sql = txtSql.Text;
                model.title = txtTitle.Text;
                model.user_id = Utils.ObjToInt(uid);
                model.role_value_list = new List<Entitys.dt_form_data_role_value>();
                model.role_value_list.AddRange(vl);
                result += (bll.Add(model) > 0 ? 1 : 0);
            }
            if (managers == result)
            {
                AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加条件" + model.title); //记录日志
                return true;
            }
            else
                return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            Entitys.dt_form_data_role model = new Entitys.dt_form_data_role();
            BLL.form_data_role bll = new BLL.form_data_role();
            List<Entitys.dt_form_data_role_value> vl = new List<Entitys.dt_form_data_role_value>();
            List<Entitys.TableRole> roles = JsonHelper.JSONToObject<List<Entitys.TableRole>>(hidWhereJson.Value);
            if (roles != null)
            {
                if (roles.Count > 0)
                {
                    foreach (Entitys.TableRole role in roles)
                    {
                        Entitys.dt_form_data_role_value r = new Entitys.dt_form_data_role_value();

                        r.add_time = r.update_time = DateTime.Now;
                        r.add_user = r.update_user = GetAdminInfo().id;
                        r.column = role.column;
                        r.delete_flag = 0;
                        r.role_id = _id;
                        r.type = role.type;
                        r.value = role.value;
                        vl.Add(r);
                    }
                }
            }

            foreach (string uid in ddlManagers.SelectedValue.Split(','))
            {
                model.id = _id;
                model.add_time = model.update_time = DateTime.Now;
                model.add_user = model.update_user = GetAdminInfo().id;
                model.delete_flag = 0;
                model.form_id = new BLL.forms().GetModel(ddlForm.SelectedValue).id;
                model.sql = txtSql.Text;
                model.title = txtTitle.Text;
                model.user_id = Utils.ObjToInt(uid);
                model.role_value_list = new List<Entitys.dt_form_data_role_value>();
                model.role_value_list.AddRange(vl);
                bll.Update(model);
            }

            AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改条件" + model.title); //记录日志
            return true;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("data_role_list", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改成功！", "data_role_list.aspx", "parent.loadMenuTree");
            }
            else //添加
            {
                ChkAdminLevel("data_role_list", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加成功！", "data_role_list.aspx", "parent.loadMenuTree");
            }
        }

        protected void ddlTable_SelectedIndexChanged(object sender, EventArgs e)
        {
            ColumnBind(sysConfig.sysdatabaseprefix + "form_data_" + ddlForm.SelectedValue);
        }
    }
}